## @package karboom.paths
# Extended paths
#
# Extensions of the karboom.classes.Path
#

from karboom.classes import Path
import cairo

## Simple path
#
# Simple vector path drawing.
#
class SimplePath(Path):
    
    ## Description of path
    description = ''

## Text path
#
# Simple path drawing using text.
#
class TextPath(Path):
    
    ## Text to render
    text = ''
    
    ## Font to use
    font = None
    
    ## Text extents
    textextents = {'ascent': 0.0, 'descent': 0.0, 'width': 0.0, 'height': 0.0, 'xadvance': 0.0, 'yadvance': 0.0}
    
    ## Font extents
    fontextents = {'xbearing': 0.0, 'ybearing': 0.0, 'height': 0.0, 'xadvance': 0.0, 'yadvance': 0.0}
    
    ## Font size to use
    fontsize = 0
    
    # Get font extents
    #
    # Get the font extents for the path without invoking __render__.
    def font_extents(self):
        tempsurf = cairo.ImageSurface(cairo.FORMAT_A1, 0, 0)
        
        tempcontext = cairo.Context(tempsurf)
        
        TextPath(context=tempcontext, **dict([(k, v) for k, v in self.__dict__.iteritems() if k != 'context' ])).__initrender__()
        
        rfe = tempcontext.font_extents()
        
        tempsurf.finish()
        
        return {'ascent': rfe[0], 'descent': rfe[1], 'height': rfe[2], 'xadvance': rfe[3], 'yadvance': rfe[4]}
    
    # Get text extents
    #
    # Get the text extents for the path without invoking __render__.
    def text_extents(self):
        tempsurf = cairo.ImageSurface(cairo.FORMAT_A1, 0, 0)
        
        tempcontext = cairo.Context(tempsurf)
        
        TextPath(context=tempcontext, **dict([(k, v) for k, v in self.__dict__.iteritems() if k != 'context' ])).__initrender__()
        
        rte = tempcontext.text_extents(self.text)
        
        tempsurf.finish()
        
        return {'xbearing': rte[0], 'ybearing': rte[1], 'width': rte[2], 'height': rte[3], 'xadvance': rte[4], 'yadvance': rte[5]}
    
    def __initrender__(self):
        self.context.select_font_face(self.font)
        self.context.set_font_size(self.fontsize)
    
    def __render__(self):
        Path.__render__(self)
        
        self.__initrender__()
        
        rfe = self.context.font_extents()
        rte = self.context.text_extents(self.text)
        
        self.fontextents = {'ascent': rfe[0], 'descent': rfe[1], 'height': rfe[2], 'xadvance': rfe[3], 'yadvance': rfe[4]}
        self.textextents = {'xbearing': rte[0], 'ybearing': rte[1], 'width': rte[2], 'height': rte[3], 'xadvance': rte[4], 'yadvance': rte[5]}
        
        self.context.move_to(0, self.fontextents['ascent'] - self.fontextents['descent']/2)
        self.context.text_path(self.text)